
/*
 基文件
 */

#import "HBaseViewController.h"
#import <CommonCrypto/CommonHMAC.h>
#import<CommonCrypto/CommonCryptor.h>
@interface HBaseViewController ()

@end

@implementation HBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = kNavBlueColor;
    self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]};
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/**
 iPhone会为每一个应用程序生成一个私有目录，这个目录位于：
 /Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下，
 并随即生成一个数字字母串作为目录名，在每一次应用程序启动时，这个字母数字串都是不同于上一次。
 所以通常使用Documents目录进行数据持久化的保存，而这个Documents目录可以通过：
 NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask，YES) 得到。
 */
-(NSString*)DocumentsDirectory{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

#pragma mark 弹窗 alert  confirm

-(void) alert:(NSString*)msg {  //callbackFn: (void(^)())callback
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //callback();
    }]];
    [self.view.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}

-(void) alertMachine:(NSString *)machine {
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请维护设备码" message:machine preferredStyle:UIAlertControllerStyleAlert];
//    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
//        //callback();
//    }]];
//
//    [self.view.window.rootViewController presentViewController:alertController animated:YES completion:nil];
//    
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:machine
//                                                    message:nil
//                                                   delegate:nil
//                                          cancelButtonTitle:@"复制"
//                                          otherButtonTitles:@"确定", nil];
//    [alert show];
//    alert.tag = 200;
//    alert.delegate = self;

}

- (void) alertMachine:(NSString *)machine callbackYesFn:(void (^)())callbackYes callbackNoFn:(void (^)())callbackNo {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:machine preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if(callbackNo){
            callbackNo();
        }
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"复制" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if(callbackYes){
            callbackYes();
        }
    }]];
    [self.view.window.rootViewController presentViewController:alertController animated:YES completion:nil];

}
-(void) confirm:(NSString*)msg callbackYesFn: (void(^)())callbackYes callbackNoFn: (void(^)())callbackNo{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if(callbackNo){
        callbackNo();
        }
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if(callbackYes){
        callbackYes();
        }
    }]];
    /*
     Warning :-Presenting view controllers on detached view controllers is discouraged     To avoid getting the warning in a push navigation, you can directly use
     self.view.window.rootViewController presentViewController
     */
    [self.view.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}

-(BOOL)fileExistsAtPath:(NSString*) filePath{
    NSFileManager* fm = [NSFileManager defaultManager];
    return [fm fileExistsAtPath:filePath];
}
-(BOOL)createDirectoryAtPath:(NSString*) directory{
    NSFileManager* fm = [NSFileManager defaultManager];
    return [fm createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
}


- (NSString *)createPrimaryID
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    NSString *user_id = [UserDefault getUserInfo].USER_ID;
    if (user_id.length > 6) {
        user_id = [[UserDefault getUserInfo].USER_ID substringFromIndex:[UserDefault getUserInfo].USER_ID.length - 6];

    }
    NSString *DateString =  [dateString stringByAppendingString:[NSString stringWithFormat:@"%@",user_id]];
    return DateString;
}
- (NSString *)getNowDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    return dateString;
}

- (NSDictionary *)getMenuParam:(NSString*) param
{
    if( [param isEqual:[NSNull null]]  || param.length == 0){
        return [[NSDictionary alloc]init];
    }
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    NSArray *paramArray = [param componentsSeparatedByString:@"&"];
    for (NSString *P in paramArray) {
        NSArray *arr = [P componentsSeparatedByString:@"="];
        [dic setObject:arr[1] forKey:[arr[0] uppercaseString]];
    }
    return dic;
    
}


- (NSString *)ChineseWithInteger:(NSInteger)integer
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = kCFNumberFormatterRoundHalfDown;
    NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:(int)integer]];
    return string;
}


#pragma mark 收键盘

-(void)dismissKeyboardWithParentView:(UIView *)parentView
{
    if(parentView){
         for (UIView *control in parentView.subviews) {
            if([control isKindOfClass:[UITextField class]] || [control isKindOfClass:[UITextView class]]){
                [control resignFirstResponder];
            }else{
                [self dismissKeyboardWithParentView:control];
            }
         }
    }
}

-(void)MyDismissKeyboardSelector{
     // 需要时
     // 1. 请在子类实现本方法
     // 2. 调用 dismissKeyboardWithParentView 参数： 文本框上一级
}

-(UIToolbar *)getDismissKeyboradToolbar{
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(2, 2, 50, 25);
    [btn addTarget:self action:@selector(MyDismissKeyboardSelector) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"keyboard_hide.png"] forState:UIControlStateNormal];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithCustomView:btn];
    NSArray * buttonsArray = [NSArray   arrayWithObjects:btnSpace,doneBtn,nil];
    [topView setItems:buttonsArray];
    return topView;
}



- (NSString *) md5:(NSString *)str {
    if (str == nil) {
        return nil;
    }
//    const char *cStr = [str UTF8String];
//    unsigned char result[16];
//    CC_MD5(cStr, strlen(cStr), result);
//    return [NSString stringWithFormat:
//            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
//            result[0], result[1], result[2], result[3],
//            result[4], result[5], result[6], result[7],
//            result[8], result[9], result[10], result[11],
//            result[12], result[13], result[14], result[15]
//            ];
    const char *cStr = [str UTF8String];
    unsigned char digest[16];
       unsigned int x=(int)strlen(cStr) ;
       CC_MD5( cStr, x, digest );
        // This is the md5 call
        NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
         for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
        return  output;
    
    
    
}

- (NSString *)getTotalTimeWithStartTime:(NSString *)startTime endTime:(NSString *)endTime{
    //按照日期格式创建日期格式句柄
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:localTimeZone];
    //将日期字符串转换成Date类型
    NSDate *startDate = [dateFormatter dateFromString:startTime];
    NSDate *endDate = [dateFormatter dateFromString:endTime];
    //将日期转换成时间戳
    NSTimeInterval start = [startDate timeIntervalSince1970]*1;
    NSTimeInterval end = [endDate timeIntervalSince1970]*1;
    NSTimeInterval value = end - start;
    //计算具体的天，时，分，秒
    int second = (int)value %60;//秒
    int minute = (int)value / 60 % 60;
    int house = (int)value / 3600;
    int day = (int)value / (24 * 3600);
    //将获取的int数据重新转换成字符串
    NSString *str;
    if (day != 0) {
        str = [NSString stringWithFormat:@"%d天%d小时%d分%d秒",day,house,minute,second];
    }else if (day==0 && house != 0) {
        str = [NSString stringWithFormat:@"%d小时%d分%d秒",house,minute,second];
    }else if (day== 0 && house== 0 && minute!=0) {
        str = [NSString stringWithFormat:@"%d分%d秒",minute,second];
    }else{
        str = [NSString stringWithFormat:@"%d秒",second];
    }
    //返回string类型的总时长
    return str;
}


@end
